package com.dangs.autotest.system;

import com.dangs.autotest.App;
import com.dangs.autotest.tool.L;

import java.lang.reflect.Method;

public class SystemProperties {

    private static final String TAG = "SystemProperties";

    private static ClassLoader getClassLoader() {
        return App.getContext().getClassLoader();
    }

    public static String get(String key, String defaultValue) {
        String ret = defaultValue;
        try {
            ClassLoader cl = getClassLoader();
            Class SystemProperties = cl.loadClass("android.os.SystemProperties");
            Class[] paramTypes = new Class[1];
            paramTypes[0] = String.class;
            Method get = SystemProperties.getMethod("get", paramTypes);
            Object[] params = new Object[1];
            params[0] = key;
            ret = (String) get.invoke(SystemProperties, params);
        } catch (IllegalArgumentException iAE) {
            L.w(TAG, "fail to ge:" + key + ", exception:" + iAE);
        } catch (Exception e) {
            L.w(TAG, "fail to ge:" + key + ", exception:" + e);
        }
        return ret;
    }

    public static Integer getInt(String key, int def) {
        Integer ret = def;
        try {
            ClassLoader cl = getClassLoader();
            Class SystemProperties = cl.loadClass("android.os.SystemProperties");
            Class[] paramTypes = new Class[2];
            paramTypes[0] = String.class;
            paramTypes[1] = int.class;
            Method getInt = SystemProperties.getMethod("getInt", paramTypes);
            Object[] params = new Object[2];
            params[0] = new String(key);
            params[1] = new Integer(def);
            ret = (Integer) getInt.invoke(SystemProperties, params);
        } catch (IllegalArgumentException iAE) {
            L.w(TAG, "fail to ge:" + key + ", exception:" + iAE);
        } catch (Exception e) {
            L.w(TAG, "fail to ge:" + key + ", exception:" + e);
        }
        return ret;
    }

    public static Boolean getBoolean(String key, boolean def) throws IllegalArgumentException {
        Boolean ret = def;
        try {
            ClassLoader cl = getClassLoader();
            Class SystemProperties = cl.loadClass("android.os.SystemProperties");
            Class[] paramTypes = new Class[2];
            paramTypes[0] = String.class;
            paramTypes[1] = boolean.class;
            Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes);
            Object[] params = new Object[2];
            params[0] = new String(key);
            params[1] = new Boolean(def);
            ret = (Boolean) getBoolean.invoke(SystemProperties, params);
        } catch (IllegalArgumentException iAE) {
            L.w(TAG, "fail to ge:" + key + ", exception:" + iAE);
        } catch (Exception e) {
            L.w(TAG, "fail to ge:" + key + ", exception:" + e);
        }
        return ret;
    }

    public static void set(String key, String val) throws IllegalArgumentException {
        try {
            Class SystemProperties = Class.forName("android.os.SystemProperties");
            Class[] paramTypes = new Class[2];
            paramTypes[0] = String.class;
            paramTypes[1] = String.class;
            Method set = SystemProperties.getMethod("set", paramTypes);

            Object[] params = new Object[2];
            params[0] = key;
            params[1] = val;
            set.invoke(SystemProperties, params);
        } catch (IllegalArgumentException iAE) {
            L.w(TAG, "fail to ge:" + key + ", exception:" + iAE);
        } catch (Exception e) {
            L.w(TAG, "fail to ge:" + key + ", exception:" + e);
        }
    }
}
